package com.bhs.sansonglogistics.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class ArcChart extends View {
    //外部传入的比例参数

    //RGB颜色数组 为渐变准备为数组，起始颜色值和终止颜色值
    private final int[] mMinColors = {Color.parseColor("#7E97FA"), Color.parseColor("#485EF4"),Color.parseColor("#7E97FA")};

    public ArcChart(Context context, float getArrPer) {
        super(context);
    }
    public ArcChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void onDraw(Canvas canvas) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;//这里是控件view的宽度，设置为了屏幕宽度
        float mMinOvalR = screenWidth / 10f; //圆环直径
        float mMinWidth = 20; //圆环宽度
        float startAngle = 90;//圆弧起始角度
        float matrixStart = 270;//渐变旋转角度

        //创建画笔：
        Paint mMinPaint = new Paint();

        mMinPaint.setAntiAlias(true); //启用抗锯齿
        mMinPaint.setDither(true); //启用抗颜色抖动（可以让渐变更平缓）
        mMinPaint.setStyle(Paint.Style.STROKE);//圆弧
        mMinPaint.setStrokeWidth(mMinWidth);//圆环宽度

        //创建画笔：
        Paint mMinPaint2 = new Paint();

        mMinPaint2.setAntiAlias(true); //启用抗锯齿
        mMinPaint2.setDither(true); //启用抗颜色抖动（可以让渐变更平缓）
        mMinPaint2.setStyle(Paint.Style.FILL);//圆弧
        mMinPaint2.setColor(Color.WHITE);



        //外接矩形
        //算出矩形顶点坐标。
        RectF rect = new RectF(canvas.getWidth() / 2 - mMinOvalR,
                canvas.getHeight() / 2 - mMinOvalR,
                canvas.getWidth() - (canvas.getWidth() / 2 - mMinOvalR),
                canvas.getHeight() - (canvas.getHeight() / 2 - mMinOvalR));
        mMinWidth=10;
        RectF rect2 = new RectF(canvas.getWidth() / 2 - mMinOvalR+mMinWidth,
                canvas.getHeight() / 2 - mMinOvalR+mMinWidth,
                canvas.getWidth() - (canvas.getWidth() / 2 - mMinOvalR)-mMinWidth,
                canvas.getHeight() - (canvas.getHeight() / 2 - mMinOvalR)-mMinWidth);
        canvas.drawArc(rect, 0, 360, false, mMinPaint);//先画一圈背景灰色圆环
        float degree = 360; //圆弧扫过的角度
        matrixStart += degree;//渐变旋转角度要开始计算
        //创建渐变
        SweepGradient mSweepGradient = new SweepGradient(canvas.getWidth() / 2,
                canvas.getHeight() / 2,
                mMinColors,
               null);
        Matrix matrix = new Matrix();//将渐变旋转
        matrix.setRotate(matrixStart, canvas.getWidth() / 2, canvas.getHeight() / 2);
        mSweepGradient.setLocalMatrix(matrix);
        //把渐变设置到笔刷
        mMinPaint.setShader(mSweepGradient);
        //画渐变的弧
        canvas.drawArc(rect, startAngle, degree, false, mMinPaint);
        canvas.drawArc(rect2, startAngle, degree, false, mMinPaint2);

    }
    private int lastX = 0;
    private int lastY = 0;
    private int beginX = 0;
    private int beginY = 0;

    private int screenWidth = 720;
    private int screenHeight = 1280;

    private void initData(Context context){
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();   // 触摸点与屏幕左边的距离
                lastY = (int) event.getRawY();   // 触摸点与屏幕上边的距离
                beginX = lastX;
                beginY = lastY;
                break;
            case MotionEvent.ACTION_MOVE:

                int dx =(int)event.getRawX() - lastX;    // x轴拖动的绝对距离
                int dy =(int)event.getRawY() - lastY;    // y轴拖动的绝对距离

                // getLeft(): 子View的左边界到父View的左边界的距离, getRight():子View的右边界到父View的左边界的距离
                // 如下几个数据表示view应该在布局中的位置
                int left = getLeft() + dx;
                int top = getTop() + dy;
                int right = getRight() + dx;
                int bottom = getBottom() + dy;
                if(left < 0){
                    left = 0;
                    right = left + getWidth();
                }
                if(right  >= screenWidth){
                right = screenWidth;
                left = right - getWidth();
            }
            if(top < 0){
                top = 0;
                bottom = top + getHeight();
            }
            if(bottom >=screenHeight){
                bottom = screenHeight;
                top = bottom - getHeight();
            }
            layout(left, top, right, bottom);
            lastX = (int) event.getRawX();
            lastY = (int) event.getRawY();
            break;
            case MotionEvent.ACTION_UP:
                // 解决拖拽的时候松手点击事件触发
                if (Math.abs(lastX - beginX) < 10 && Math.abs(lastY - beginY) < 10){
                    return super.onTouchEvent(event);
                }else{
                    setPressed(false);
                    return true;
                }
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

}